﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sim
{
    class DbHelper
    {
        public readonly static string connStr = "server=.;database=Blogs;Uid=sa;Pwd=123456;";//连接字符串
        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <param name="sql">select语句</param>
        /// <returns>表数据</returns>
        public static DataTable GetDbHelper(string sql)
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, connStr);

            DataTable dt = new DataTable();

            dataAdapter.Fill(dt);

            return dt;
        }

        //定义方法执行增删改
        public static bool AddOrInsertOrUpdate(string sql)
        {        
            //创建数据库连接对象
            SqlConnection conn = new SqlConnection(connStr);

            //打开数据库连接
            conn.Open();

            //创建command对象并执行sql语句
            SqlCommand cmd = new SqlCommand(sql, conn);

            int result = cmd.ExecuteNonQuery();

            //定义返回值
            return result > 0;
        }
    }
}
